
import Pure from 'pure-frontend';
import { globalUtils } from '../../../common/js/global-utils/global-utils-all';
import { uiUtils } from '../../../common/js/ui-utils/ui-utils-all';
/* SOURCE-CODE-START */

"use strict";

Pure.app.viewMain('example2-new', function (scope, view) {
  var model = view.getDataModel();

  // 定义校验规则
  view.$find('form').validate({
    rules: {
      exampleName: 'required'
    },
    messages: {
      exampleName: '${t("ui.example.example2.validation.inputExampleName")}'
    }
  });

  view.$ui('backBtn').on('click', function () {
    Pure.app.popView('app/example2/search');
  });

  // 点击保存按钮的事件
  view.$ui('saveBtn').on('click', function () {
    var checkResult = view.$find('form').valid();
    if (!checkResult) {
      return;
    }

    var saveData = model.getData('*');

    view.callService('api/example/saveDetail', saveData)
      .waitResult(function (result, textStatus, jqXHR) {
        if (result.success) {
          Pure.app.showMessage('${t("ui.system.common.msg.saveSuccess")}');

          var id = result.data.id;
          var url = globalUtils.formatString('app/example2/edit/{0}', id);
          Pure.app.replaceView(url);
        } else {
          Pure.app.showError(result.message);
        }
      });
  });

  uiUtils.view.setCurrentFormDataToContext(scope, getFormData);
  // 离开时检查表单数据是否未保存
  uiUtils.view.checkFormDataChangeWhenLeave(scope, getFormData);

  function getFormData() {
    return model.getData('*');
  }

});

/* SOURCE-CODE-END */
